﻿using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms.Design;
using XCI.Windows;

namespace XCI.Core
{
    //[Designer(typeof(MyTextBoxDesigner))]
    [System.ComponentModel.DesignerCategory("Code")]
    public partial class AreaComponent : Component
    {
        private readonly AreaComboBoxEditManager manager = new AreaComboBoxEditManager();

        public AreaComponent()
        {
            InitializeComponent();
        }

        public AreaComponent(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }

        /// <summary>
        /// 省份下拉控件
        /// </summary>
        [Category("控件"), Description("省份下拉控件"), DefaultValue(null)]
        public ComboBoxEditx ProvinceEdit { get; set; }

        /// <summary>
        /// 城市下拉控件
        /// </summary>
        [Category("控件"), Description("城市下拉控件"), DefaultValue(null)]
        public ComboBoxEditx CityEdit { get; set; }

        /// <summary>
        /// 区县下拉控件
        /// </summary>
        [Category("控件"), Description("区县下拉控件"), DefaultValue(null)]
        public ComboBoxEditx CountyEdit { get; set; }

        /// <summary>
        /// 街道下拉控件
        /// </summary>
        [Category("控件"), Description("街道下拉控件"), DefaultValue(null)]
        public ComboBoxEditx TownEdit { get; set; }

        /// <summary>
        /// 省份标识字段
        /// </summary>
        [Category("对象"), Description("省份标识字段"), DefaultValue(null)]
        public string ProvinceIdField { get; set; }

        /// <summary>
        /// 省份名称字段
        /// </summary>
        [Category("对象"), Description("省份名称字段"), DefaultValue(null)]
        public string ProvinceNameField { get; set; }

        /// <summary>
        /// 城市标识字段
        /// </summary>
        [Category("对象"), Description("城市标识字段"), DefaultValue(null)]
        public string CityIdField { get; set; }

        /// <summary>
        /// 城市名称字段
        /// </summary>
        [Category("对象"), Description("城市名称字段"), DefaultValue(null)]
        public string CityNameField { get; set; }

        /// <summary>
        /// 区县标识字段
        /// </summary>
        [Category("对象"), Description("区县标识字段"), DefaultValue(null)]
        public string CountyIdField { get; set; }

        /// <summary>
        /// 区县名称字段
        /// </summary>
        [Category("对象"), Description("区县名称字段"), DefaultValue(null)]
        public string CountyNameField { get; set; }

        /// <summary>
        /// 街道标识字段
        /// </summary>
        [Category("对象"), Description("街道标识字段"), DefaultValue(null)]
        public string TownIdField { get; set; }

        /// <summary>
        /// 街道名称字段
        /// </summary>
        [Category("对象"), Description("街道名称字段"), DefaultValue(null)]
        public string TownNameField { get; set; }

        /// <summary>
        /// 读取或者设置控件值
        /// </summary>
        [Browsable(false), DefaultValue(null), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public AreaValue EditValue
        {
            get => manager.EditValue;
            set => manager.EditValue = value;
        }

        /// <summary>
        /// 取值和设值数据对象
        /// </summary>
        [Browsable(false), DefaultValue(null), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public object DataObject
        {
            get => manager.DataObject;
            set => manager.DataObject = value;
        }

        public void Initialize()
        {
            //绑定控件
            manager.ProvinceEdit = ProvinceEdit;
            manager.CityEdit = CityEdit;
            manager.CountyEdit = CountyEdit;
            manager.TownEdit = TownEdit;

            //绑定DataObject中的取值/设值字段
            manager.ProvinceIdField = ProvinceIdField;
            manager.ProvinceNameField = ProvinceNameField;
            manager.CityIdField = CityIdField;
            manager.CityNameField = CityNameField;
            manager.CountyIdField = CountyIdField;
            manager.CountyNameField = CountyNameField;
            manager.TownIdField = TownIdField;
            manager.TownNameField = TownNameField;
        }
    }

    public class MyTextBoxActionList : DesignerActionList
    {
        /// <summary>
        /// 定一个要自定义标签的控件
        /// </summary>
        private readonly AreaComponent _textBox;

        public MyTextBoxActionList(IComponent component) : base(component)
        {
            this._textBox = component as AreaComponent;

        }

        /// <summary>
        /// 该控件的属性
        /// </summary>
        public ComboBoxEditx ProvinceEdit
        {
            get
            {
                return _textBox.ProvinceEdit;
            }
            set
            {
                _textBox.ProvinceEdit = value;
            }
        }

        /// <summary>
        /// 重写父类的方法，实现在智能标签显示的属性
        /// </summary>
        /// <returns></returns>
        public override DesignerActionItemCollection GetSortedActionItems()
        {
            DesignerActionItemCollection items = new DesignerActionItemCollection();
            //items.Add(new DesignerActionPropertyItem("ProvinceEdit", "省份控件"));

            return items;
        }

    }

    public class MyTextBoxDesigner : ParentControlDesigner
    {
        private DesignerActionListCollection _actionLists;

        public override System.ComponentModel.Design.DesignerActionListCollection ActionLists => _actionLists ?? (_actionLists = new DesignerActionListCollection {new MyTextBoxActionList(this.Component)});
    }
}
